package com.wanqing.community.manager.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.wanqing.community.bean.MenuBean;
import com.wanqing.community.bean.MenuOperateBean;
import com.wanqing.community.dao.MenuDao;
import com.wanqing.community.dao.MenuOperateDao;
import com.wanqing.community.manager.MenuManager;
import com.wanqing.community.util.CommunityException;
import com.wanqing.community.util.ConditionParse;
import com.wanqing.community.util.NumberDealTools;
import com.wanqing.community.util.StringTools;
import com.wanqing.community.bean.JsonTree;

/**
 * 菜单管理业务接口实现类
 */
@Service("menuManager")
public class MenuManagerImpl implements MenuManager{
	
	private static Logger log = Logger.getLogger(MenuManagerImpl.class);
	
	@Resource
	private MenuDao menuDao;
	
	@Resource
	private MenuOperateDao menuOperateDao;

	
	/**
	 * <p>Description:根据条件得到菜单的数量<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public int getMenuCount(ConditionParse condParse) throws CommunityException {		
		int total = 0;
		try{
			total = this.menuDao.getMenuCount(condParse);
		}catch (CommunityException e) {
			log.error("根据条件得到菜单的数量异常："+e.toString());
			throw e;
		}
		return total;
	}

	/**
	 * <p>Description:根据查询条件查询菜单列表<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public List<MenuBean> queryMenu(ConditionParse condParse)
			throws CommunityException {	
		List<MenuBean> list = null;
		try{
			list = this.menuDao.queryMenu(condParse);
		}catch (CommunityException e) {
			log.error("根据查询条件查询菜单列表异常："+e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * <p>Description:查询菜单数据产生extjs的导航菜单<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 */
	public List<JsonTree> queryNavigationMenu(ConditionParse condParse) {
		List<JsonTree> trees = new ArrayList<JsonTree>();
		List<MenuBean> list = this.queryMenu(condParse);
		JsonTree tree = null;
		if(null!=list && !list.isEmpty()){
			for (MenuBean menu : list) {
				tree = new JsonTree();
				tree.setIcon(menu.getMicon());			
				tree.setUrl(menu.getMurl());
				tree.setId(menu.getMid());
				tree.setLeaf(menu.isMleaf());
				tree.setText(menu.getMname());
				tree.setIconCls(menu.getIconCls());
				tree.setCode(menu.getMcode());
				tree.setPid(menu.getMpid());
				tree.setDepth(menu.getMdepth());				
				trees.add(tree);
			}
		}
		return trees;
	}
	
	public boolean addMenu(MenuBean mb) throws CommunityException {
		int i = 0;
		try{			
			List<MenuOperateBean> listoperates = mb.getListoperates();
			MenuBean parent = mb.getParentMenu();
			String code = this.queryMenuCode(mb.getMpid(), parent.getMcode());		
			int mdepth = parent.getMdepth()+1;
			mb.setMcode(code);
			//默认当前节点为叶子节点
			boolean mleaf = true;
			boolean hasOperate = false;
			if(null!=listoperates && !listoperates.isEmpty() && listoperates.size()>0){				
				hasOperate = true;
			}
			mb.setMleaf(mleaf);
			mb.setMdepth(mdepth);
			mb.setHasOperate(hasOperate);
			i = menuDao.addMenu(mb);
			//修改当前节点的上级节点为根节点
			menuDao.updateMenuLeaf(false, mb.getMpid());
			menuOperateDao.addMenuOperate(listoperates);			
		}catch (CommunityException e) {
			log.error("添加菜单时出现异常信息："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}

	/**
	 * <p>Description:删除菜单<p>
	 * <p>modifiContent:<p>
	 * @param list
	 * @return
	 * @throws CommunityException
	 */
	public boolean deleteMenu(List<String> list) throws CommunityException {
		int i = 0;
		try{
			this.menuOperateDao.deleteMenuOperate(list);
			i = this.menuDao.deleteMenu(list);
		}catch (CommunityException e) {
			log.error("删除菜单异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}

	/**
	 * <p>Description:修改菜单信息<p>
	 * <p>modifiContent:<p>
	 * @param menu
	 * @return
	 * @throws CommunityException
	 */
	public boolean updateMenu(MenuBean menu) throws CommunityException {
		int i = 0;
		try{			
			List<MenuOperateBean> listoperates = menu.getListoperates();
			boolean hasOperate = false;
			if(null!=listoperates && !listoperates.isEmpty() && listoperates.size()>0){				
				hasOperate = true;
				ConditionParse condParse = new ConditionParse();
				for (MenuOperateBean menuOperateBean : listoperates) {
					condParse.deleteAll();
					String operId = menuOperateBean.getOperId();
					condParse.addCondition("OPERID", "=", operId);
					condParse.addWhereStr();
					int total = this.menuOperateDao.getMenuOperateCount(condParse);
					if(total==0){
						this.menuOperateDao.addMenuOperate(menuOperateBean);
					}else{
						this.menuOperateDao.updateMenuOperate(menuOperateBean);
					}
				}
			}
			menu.setHasOperate(hasOperate);
			i = this.menuDao.updateMenu(menu);
		}catch (CommunityException e) {
			log.error("修改菜单时出现异常信息："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
	/**
	 * <p>Description:<p>
	 * <p>modifiContent:<p>
	 * @param parentId
	 * @param parentCode
	 * @return
	 */
	private String queryMenuCode(String parentId,String parentCode){
		ConditionParse condParse = new ConditionParse();
		condParse.addCondition("mpid", "=", parentId);		
		condParse.addWhereStr();
		String pcode = StringTools.objectToString(parentCode);
		int total = this.getMenuCount(condParse);
		int forObj = total+1;		
		String temp = NumberDealTools.format(forObj, 3);		
		String result = pcode+temp;			
		return result;
	}

	/**
	 * <p>Description:查询所有的菜单信息<p>
	 * <p>modifiContent:<p>
	 * @return
	 * @throws CommunityException
	 */
	private List<MenuBean> listMenu() throws CommunityException {
		List<MenuBean> list = null;
		try{
			list = this.menuDao.listMenu();
		}catch (CommunityException e) {
			log.error("查询所有的菜单信息异常："+e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * <p>Description:通过ID查找菜单信息<p>
	 * <p>modifiContent:<p>
	 * @param id
	 * @return
	 * @throws CommunityException
	 */
	public MenuBean findMenuById(String id) throws CommunityException {
		MenuBean menu = null;
		try{
			menu = this.menuDao.findMenuById(id);
		}catch (CommunityException e) {
			log.error("通过ID查找菜单信息异常："+e.toString());
			throw e;
		}		
		return menu;
	}
	
	/**
	 * <p>Description:分页查询菜单信息<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @param currPage 当前页
	 * @param pageSize 每页显示的记录数
	 * @return
	 * @throws CommunityException
	 */
	public List<MenuBean> queryMenu(ConditionParse condParse, int currPage,
			int pageSize) throws CommunityException {		
		List<MenuBean> list = null;
		try{
			list = this.menuDao.queryMenu(condParse, currPage, pageSize);
		}catch (CommunityException e) {
			log.error("分页查询菜单信息异常："+e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * <p>Description:查询菜单对应的操作功能<p>
	 * <p>modifiContent:<p>
	 * @param condParse
	 * @return
	 * @throws CommunityException
	 */
	public List<MenuOperateBean> querMenuOperate(ConditionParse condParse)
			throws CommunityException {
		List<MenuOperateBean> list = null;
		try{
			list = this.menuOperateDao.queryMenuOperate(condParse);
		}catch (CommunityException e) {
			log.error("查询菜单对应的操作功能异常："+e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * <p>Description:查询所有的菜单以及对应的按钮，业务功能<p>
	 * <p>modifiContent:<p>
	 * @return
	 */
	public List<MenuBean> listAuthorMenu() {
		List<MenuBean> list = null;
		try{
			list = this.menuDao.listAuthorMenu();
		}catch (CommunityException e) {
			log.error("查询所有的菜单以及对应的按钮，业务功能异常："+e.toString());
			throw e;
		}
		return list;
	}

	/**
	 * <p>Description:查询所有的权限菜单菜单信息<p>
	 * <p>modifiContent:原参数<p>
	 * @param roleId 角色ID
	 * @return
	 * @throws CommunityException
	 */
	public List<MenuBean> queryAclMenu(String[] roleIds) throws CommunityException {
		List<MenuBean> list = null;
		try{
			boolean b = Arrays.asList(roleIds).contains("-1");
			if(b){
				list = this.listMenu();
			}else{
				ConditionParse parse = new ConditionParse();
				parse.addInCondition("T.ROLEID", "in", roleIds);
				parse.addWhereStr();
				list = this.menuDao.queryMenuAcl(parse);
			}
		}catch (CommunityException e) {
			log.error("查询所有的权限菜单菜单信息异常:"+e.toString());
			throw e;
		}
		return list;
	}
	
	private List<MenuBean> queryAclMenuAndOper(String roleId, String operType)
			throws CommunityException {		
		List<MenuBean> list = null;
		try{
			Map<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("roleId", roleId);
			paramsMap.put("operType", operType);
			list = this.menuDao.queryAclMenuAndOper(paramsMap);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
		return list;
	}
	
	public Map<String, List<MenuOperateBean>> queryBuisOperate(String roleId)
			throws CommunityException {		
		try{
			List<MenuBean> list = this.queryAclMenuAndOper(roleId, "BUSINE");
			return this.operateAcl(list);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
	}
	
	public Map<String, List<MenuOperateBean>> queryFuncOperate(String roleId)
			throws CommunityException {
		try{
			List<MenuBean> list = this.queryAclMenuAndOper(roleId, "FUNTREE");
			return this.operateAcl(list);
		}catch (CommunityException e) {
			log.error(e.toString());
			throw e;
		}
	}
	
	private Map<String, List<MenuOperateBean>> operateAcl(List<MenuBean> list){
		Map<String, List<MenuOperateBean>> map = new HashMap<String, List<MenuOperateBean>>();
		if(null!=list && !list.isEmpty() && list.size()>0){
			for (MenuBean mb : list) {
				String menuId = mb.getMid();
				List<MenuOperateBean> listoperate = mb.getListoperates();
				if(null!=listoperate && !listoperate.isEmpty() && listoperate.size()>0){
					map.put(menuId, listoperate);
				}
			}
		}
		return map;
	}
	
	/**
	 * <p>Description:删除子菜单<p>
	 * <p>modifiContent:<p>
	 * @param list
	 * @return
	 * @throws CommunityException
	 */
	public boolean deleteMenuOperate(String menuId) throws CommunityException {
		int i = 0;
		try{
			i = this.menuOperateDao.deleteMenuOperateByMenuId(menuId);
		}catch (CommunityException e) {
			log.error("删除子菜单异常："+e.toString());
			throw e;
		}
		return i>0?true:false;
	}
	
}
